package com.dwen.fakewx.mvp.view;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.dwen.fakewx.R;
import com.dwen.fakewx.mvp.BaseActivity;

import butterknife.BindView;


public class MainActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.home_tv1)
    TextView home_tv1;
    @BindView(R.id.home_tv2)
    TextView home_tv2;
    @BindView(R.id.home_tv3)
    TextView home_tv3;
    @BindView(R.id.home_tv4)
    TextView home_tv4;
    @BindView(R.id.rootLayout)
    FrameLayout rootLayout;

    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private ChatFragment fragment1;
    private ContactFragment fragment2;
    private DetectFragment fragment3;
    private MyFragment fragment4;

    @Override
    public int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        HomeBar homeBar = new HomeBar(this);
        rootLayout.addView(homeBar.getLayout(),0);

        fragmentManager = getFragmentManager();

        home_tv1.setOnClickListener(this);
        home_tv2.setOnClickListener(this);
        home_tv3.setOnClickListener(this);
        home_tv4.setOnClickListener(this);
        initFragment();
        setTabSelection(0);
        home_tv1.setSelected(true);
    }


    private void initFragment() {
        fragment1 = new ChatFragment().newInstance("fragment1", "blank");
        fragment2 = new ContactFragment().newInstance("fragment2", "blank");
        fragment3 = new DetectFragment().newInstance("fragment3", "blank");
        fragment4 = new MyFragment().newInstance("fragment4", "blank");

        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.content, fragment1, "chats");
        transaction.add(R.id.content, fragment2, "contacts");
        transaction.add(R.id.content, fragment3, "detect");
        transaction.add(R.id.content, fragment4, "my");

        transaction.commit();


    }

    private void setTabSelection(int index) {
        transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        switch (index) {
            case 0:
                if (fragment1 == null) {
                    fragment1 = new ChatFragment().newInstance("fragment1", "blank");
                    transaction.add(R.id.content, fragment1);
                } else {
                    transaction.show(fragment1);
                }


                break;
            case 1:
                if (fragment2 == null) {
                    fragment2 = new ContactFragment().newInstance("fragment2", "blank");
                    transaction.add(R.id.content, fragment2);
                } else {
                    transaction.show(fragment2);
                }
                break;
            case 2:
                if (fragment3 == null) {
                    fragment3 = new DetectFragment().newInstance("fragment3", "blank");
                    transaction.add(R.id.content, fragment3);
                } else {
                    transaction.show(fragment3);
                }
                break;
            case 3:
                if (fragment4 == null) {
                    fragment4 = new MyFragment().newInstance("fragment4", "blank");
                    transaction.add(R.id.content, fragment4);
                } else {
                    transaction.show(fragment4);
                }
                break;
        }
        transaction.commitAllowingStateLoss();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (fragment1 != null) {
            transaction.hide(fragment1);
        }
        if (fragment2 != null) {
            transaction.hide(fragment2);
        }
        if (fragment3 != null) {
            transaction.hide(fragment3);
        }
        if (fragment4 != null) {
            transaction.hide(fragment4);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.home_tv1:
                setTabSelection(0);
                home_tv1.setSelected(true);
                home_tv2.setSelected(false);
                home_tv3.setSelected(false);
                home_tv4.setSelected(false);
                break;
            case R.id.home_tv2:
                setTabSelection(1);
                home_tv1.setSelected(false);
                home_tv2.setSelected(true);
                home_tv3.setSelected(false);
                home_tv4.setSelected(false);
                break;
            case R.id.home_tv3:
                setTabSelection(2);
                home_tv1.setSelected(false);
                home_tv2.setSelected(false);
                home_tv3.setSelected(true);
                home_tv4.setSelected(false);
                break;
            case R.id.home_tv4:
                setTabSelection(3);
                home_tv1.setSelected(false);
                home_tv2.setSelected(false);
                home_tv3.setSelected(false);
                home_tv4.setSelected(true);
                break;
        }
    }
}
